Pass OpenAPI document to Context class constructor - #1638
Pass OpenAPI document to Context class constructor#1638pmcelhaney with Copilot wants to merge 22 commits into
Conversation
🦋 Changeset detectedLatest commit: 7545c07 The changes in this PR will be included in the next version bump. This PR includes changesets to release 1 package
Not sure what this means? Click here to learn what changesets are. Click here if you're a maintainer who wants to add another changeset to this PR |
pmcelhaney
left a comment
There was a problem hiding this comment.
If the document is changed on disk make sure the object passed to the context object receives the updates.
One way to do that is to remove all top level keys and replace them.
Done in commit 2cfdd55. In |
pmcelhaney
left a comment
There was a problem hiding this comment.
Maybe it would be smarter to assign openApiDocument reference to a Proxy that is read only.
Done in commit 4c25b46. The |
pmcelhaney
left a comment
There was a problem hiding this comment.
The .current property seems unnecessary. The object could just be assigned to openApiDocumentRef.
I don't understand why you wouldn't update the openApiDocument if it wasn't assigned originally. Make openApiDocumentRef default to an empty object if necessary.
If there are good reasons for either of these decisions, don't make changes. Just explain the reasoning.
Done in commit a20b89c.
|
|
@copilot TypeScript compilation is failing src/server/module-loader.ts:53:5 - error TS2741: Property 'paths' is missing in type '{}' but required in type 'OpenApiDocument'. 53 openApiDocument: OpenApiDocument = {}, src/server/dispatcher.ts:60:3 Found 1 error in src/server/module-loader.ts:53 |
|
@copilot resolve conflicts |
Done in commits e6096f0 and bbe62d6. The merge was already in place but it introduced a TypeScript error: making |
|
@copilot there are still merge conflicts |
…ide ScenarioRegistry
Done in commit 023f8b7. Merged
|
|
Somehow the call stack is getting blown when I try to use this. It's not a very important feature right now so I'm going to let it go. |
Summary
The
Contextclass in_.context.tshad no way to access the loaded OpenAPI document at construction time. This change passes it asopenApiDocumentin the constructor options object, alongside the existingloadContextandreadJsonhelpers. When the OpenAPI spec changes on disk, theOpenApiDocumentclass updates its own properties in-place (viawatch()→load()), and the Context automatically sees the latest spec through a stable read-onlyProxy.Original Prompt
Pass the loaded OpenAPI document to the
Contextclass constructor so route handlers can access spec metadata at runtime.Manual acceptance tests
_.context.tsconstructor receivesopenApiDocumentand it matches the loaded spec (e.g.openApiDocument.pathscontains the routes defined in the YAML)_.context.tsfiles with no constructor argument continue to work unchangedloadContextandreadJsonstill work correctly alongsideopenApiDocumentopenApiPath === "_"),openApiDocumentis an empty object — notundefinedor an erroropenApiDocumentreflects the updated spec through the same proxy referenceopenApiDocumentinside a Context does not throw and has no effect (read-only)Tasks
openApiDocument?: OpenApiDocumentparameter (6th position, afterscenariosPathandscenarioRegistry) toModuleLoaderconstructor; importedOpenApiDocumenttype fromopenapi-document.tsProxywrapping the providedOpenApiDocumentinstance (or{}when none is given); since theOpenApiDocumentclass updates its own properties in-place on reload, noopenApiDocumentRefindirection orsetOpenApiDocumentmethod is neededProxyreturnsfalseforsetanddeletePropertytraps, making it read-only; reads delegate to the underlying object so in-place updates are immediately visiblethis.openApiDocumentProxyinto the Context constructor options objectapp.ts, forwardedopenApiDocumentas the 6th argument toModuleLoaderin bothcounterfact()andcreateMswHandlers(); removed the now-redundantcodeGenerator.generateevent listener (spec reloads are handled byopenApiDocument?.watch())OpenApiDocumentinterface fromdispatcher.ts; kept main's required-pathsversionorigin/mainchanges:ScenarioRegistrysupport inModuleLoader,writeApplyContextTypeon context changes,openApiDocument?.watch()/openApiDocument?.stopWatching()lifecycle, and the newOpenApiDocumentclass inopenapi-document.tssrc/repl/route-builder.ts: extractedpaths ?? {}to a local variable beforeObject.keys()so the loop body always operates on a defined valueModuleLoaderconstructor call positions (undefined, undefined, openApiDocument), replacedsetOpenApiDocumenttests with in-place mutation tests verifying the proxy reflects document changes; added tests "reflects in-place document updates through the proxy" and "proxy reflects mutated document properties"